package http_servicer

import (
	//	"encoding/json"
	"fmt"
	"ifms/db_servicer"
	"net/http"
	"strconv"
)

// This script implement the function related with device information,
//  including  information in table device_list, device_info_hist
//
//	router.HandleFunc("/newdevice",        newdeviceHandler).Methods("POST")
//	router.HandleFunc("/modifydevice",     modifydeviceHandler).Methods("POST")
//	router.HandleFunc("/devicelist",       devicelistHandler).Methods("POST")

func newdeviceHandler(response http.ResponseWriter, request *http.Request) {
	// logger = log.New(os.Stdout, ">>>>>>>> New Device:", log.LstdFlags|log.Lshortfile)

	v_device_id := request.FormValue("device_id")
	v_device_type := request.FormValue("device_type")
	v_SIM := request.FormValue("SIM")
	v_pond_id, _ := strconv.Atoi(request.FormValue("pond_id"))

	// logger.Println("device_id:" , v_device_id,",device_type:", v_device_type,
	// ",SIM:", v_SIM,",pond_id:", v_pond_id  )

	dbmsg := db_servicer.New_Device(v_device_id, v_device_type, v_SIM, v_pond_id)
	// logger.Println("=============Back from DB_SERVERIC.New_Device==============")
	// send2Device(v_device_id)
	chan2Device <- v_device_id

	logger.Println("\n Dbmsg:", dbmsg)
	fmt.Fprint(response, dbmsg.Data)

}

func devicelistHandler(response http.ResponseWriter, request *http.Request) {
	// logger = log.New(os.Stdout, ">>>>>>> Device List:", log.LstdFlags|log.Lshortfile)

	v_session_id, v_user_id := getAccountName(request)

	logger.Println(">>>session_id:", v_session_id, ", user_id :", v_user_id)

	if v_user_id == 0 {
		reloginPage(response, request)
		return
	}

	//	var responseStr []byte

	dbmsg := db_servicer.Device_List(v_user_id)

	// logger.Println("=============Back from DB_SERVERIC.Device_List==============")
	logger.Println("\n Dbmsg:", dbmsg)
	fmt.Fprint(response, dbmsg.Data)
	/*
	   	if dbmsg.Flag == 0 {
	   		//fetched device list

	   		var device_list  [] map[string]string

	   		if err := json.Unmarshal( []byte(dbmsg.Message), &device_list  ); err != nil{
	   			log.Panic(err)
	   		}

	   		msg := retMsg{ Flag: 0 , Message: "成功读取设备信息",  Data: device_list }
	   		responseStr, _ = json.Marshal(msg)
	   		fmt.Fprint(response, string(responseStr))

	   	}else {
	   		//fetch  Fail
	   		msg := simple_retMsg{ Flag: dbmsg.Flag,  Message: dbmsg.Message }
	   		responseStr, _ = json.Marshal(msg)
	   		fmt.Fprint(response, string(responseStr))
	   	}

	   //	logger.Println("dbmsg:", dbmsg)
	   	logger.Println(">>>Return to Client:", string(responseStr))
	*/

}

func modifydeviceHandler(response http.ResponseWriter, request *http.Request) {
	// logger = log.New(os.Stdout, ">>>>>>> Modify Device :", log.LstdFlags|log.Lshortfile)

	v_session_id, v_user_id := getAccountName(request)

	logger.Println(">>>session_id:", v_session_id, ", user_id :", v_user_id)

	if v_user_id == 0 {
		reloginPage(response, request)
		return
	}

	device_id := request.FormValue("device_id")
	device_type := request.FormValue("device_type")
	o_device_type := request.FormValue("o_device_type")
	SIM := request.FormValue("SIM")
	o_SIM := request.FormValue("o_SIM")
	status := request.FormValue("status")
	o_status := request.FormValue("o_status")

	v_pond_id := request.FormValue("pond_id")
	v_o_pond_id := request.FormValue("o_pond_id")

	var pond_id, o_pond_id int

	var msg string = "Not an valid nubmer:"
	var err error

	if pond_id, err = strconv.Atoi(v_pond_id); err != nil && v_pond_id != "" {
		msg = msg + " pond_id  :" + v_pond_id
	}
	if o_pond_id, err = strconv.Atoi(v_o_pond_id); err != nil && v_o_pond_id != "" {
		msg = msg + " o_pond_id:" + v_o_pond_id
	}

	if msg > "Not an valid nubmer:" {
		logger.Println("\nError:", err)
		logger.Println("\nMsg:", msg)
		fmt.Fprintf(response, `{"flag":-1,"message":"%s" }`, msg)
	} else {

		// logger.Println( "'\ndevice_id    :'",device_id    ,
		// 				"\npond_id      :'",pond_id      ,
		// 				"',o_pond_id    :'",o_pond_id    ,
		// 				"'\ndevice_type  :'",device_type  ,
		// 				"',o_device_type:'",o_device_type,
		// 				"'\nSIM          :'",SIM          ,
		// 				"',o_SIM        :'",o_SIM        ,
		// 				"'\nstatus       :'",status       ,
		// 				"',o_status     :'",o_status     , "'")

		dbmsg := db_servicer.Modify_Device(v_user_id, pond_id, o_pond_id, device_id, device_type, o_device_type, SIM, o_SIM, status, o_status)

		logger.Println("\n Dbmsg:", dbmsg)
		fmt.Fprint(response, dbmsg.Data)

	}

}
